﻿Imports System
Imports System.Resources
Imports System.Threading
Imports System.Reflection
Imports System.Globalization
Imports System.Collections

'Namespace MMUtil
Public Class clsLocalization
    Private m_ResourceMgr As ResourceManager
    Private m_ciDefault As CultureInfo
    Private m_strResourcePath As String

    Sub New(ByVal strResourcePath As String, ByRef ciDefault As CultureInfo)
        m_strResourcePath = strResourcePath
        m_ResourceMgr = New ResourceManager(strResourcePath, Assembly.GetCallingAssembly)
        m_ciDefault = CType(ciDefault.Clone(), CultureInfo)
    End Sub

    Public Sub SetCultureInfo(ByVal ci As CultureInfo)
        m_ciDefault = ci
    End Sub

    'Protected Overrides Sub Finalize()
    '    m_ResourceMgr = Nothing
    '    Finalize()
    'End Sub

    Private Sub TranslateText(ByRef obj As Control, Optional ByVal TrasCode As String = Nothing, Optional ByRef ci As CultureInfo = Nothing)
        Try
            If ci Is Nothing Then ci = m_ciDefault

            If obj.Tag Is Nothing Then Return

            If TypeOf obj Is TextBox Or _
                TypeOf obj Is Label Or _
                TypeOf obj Is Button Or _
                TypeOf obj Is GroupBox Or _
                TypeOf obj Is CheckBox Or _
                TypeOf obj Is Form Or _
                TypeOf obj Is TabPage Then

                If (Not TrasCode Is Nothing) Then
                    obj.Text = m_ResourceMgr.GetString(TrasCode, ci)
                Else
                    obj.Text = m_ResourceMgr.GetString(obj.Tag.ToString, ci)
                End If


            ElseIf TypeOf obj Is PictureBox Then
                CType(obj, PictureBox).ImageLocation = Application.StartupPath + m_ResourceMgr.GetString(obj.Tag.ToString, ci)
                CType(obj, PictureBox).Load()
            End If

        Catch ex As Exception
            SetErrorInfo(ex)
        End Try

    End Sub

    Public Sub Translate(ByRef RootObj As Object, Optional ByRef ci As CultureInfo = Nothing)
        Try
            ClearError()
            For Each obj As Control In RootObj.Controls
                If obj.HasChildren Then
                    Translate(obj, ci)
                Else
                    TranslateText(obj, Nothing, ci)
                End If
            Next

            TranslateText(RootObj, Nothing, ci)

        Catch ex As Exception
            SetErrorInfo(ex)
        End Try
    End Sub

    Public Sub Translate(ByRef Obj As Control, ByVal TransCode As String, Optional ByRef ci As CultureInfo = Nothing)
        Try
            ClearError()
            TranslateText(Obj, TransCode, ci)

        Catch ex As Exception
            SetErrorInfo(ex)
        End Try
    End Sub

    Public Function Translate(ByVal TransCode As String, Optional ByRef ci As CultureInfo = Nothing)
        Try
            ClearError()
            If ci Is Nothing Then ci = m_ciDefault

            Translate = m_ResourceMgr.GetString(TransCode, ci)

        Catch ex As Exception
            Translate = ""
            SetErrorInfo(ex)
        End Try

    End Function

End Class

Public Class clsGeneral

    Enum ControlType
        CT_BUTTON = 0
        CT_TEXTBOX = 1
        CT_LABEL = 2
        CT_CHECKBOX = 3
        CT_GROUPBBOX = 4
        CT_TABPAGE = 5
        CT_COMBOBOX = 6
    End Enum


    Public Shared Sub ClearObjectText(ByRef Container As Control, ByVal SearchName As String, ByVal SearchType As ControlType)
        Dim arr As ArrayList

        arr = FindObject(Container, SearchName, SearchType)

        For Each obj In arr
            CType(obj, Control).Text = ""
        Next

    End Sub

    Public Shared Function FindSingleObject(ByRef Container As Control, ByVal SearchName As String, ByVal ArrIndex As Integer) As Control
        Dim strNewSearchName As String
        Dim obj As Object
        Dim objType As Type

        '_txtDead_9
        strNewSearchName = "_" + SearchName + "_" + ArrIndex.ToString()

        obj = CType(Container, ContainerControl).Controls(strNewSearchName)

        If Not obj Is Nothing Then
            objType = CType(Container, ContainerControl).Controls(strNewSearchName).GetType()

            If TypeOf obj Is Button Then
                FindSingleObject = CType(obj, Button)
            ElseIf TypeOf obj Is TextBox Then
                FindSingleObject = CType(obj, TextBox)
            ElseIf TypeOf obj Is Label Then
                FindSingleObject = CType(obj, Label)
            ElseIf TypeOf obj Is CheckBox Then
                FindSingleObject = CType(obj, CheckBox)
            ElseIf TypeOf obj Is GroupBox Then
                FindSingleObject = CType(obj, GroupBox)
            ElseIf TypeOf obj Is TabPage Then
                FindSingleObject = CType(obj, TabPage)
            ElseIf TypeOf obj Is ComboBox Then
                FindSingleObject = CType(obj, ComboBox)
            End If
        Else
            FindSingleObject = Nothing
        End If

    End Function


    Public Shared Function FindSingleObject(ByRef Container As Control, ByVal SearchName As String) As Control

        Dim strNewSearchName As String

        '_txtDead_9
        If SearchName.IndexOf("(") > 0 Then
            Dim strArr() As String
            strArr = SearchName.Split("(")
            strNewSearchName = "_" + strArr(0) + "_" + strArr(1).Substring(0, strArr(1).Length - 1)
        Else
            strNewSearchName = SearchName
        End If

        FindSingleObject = CType(Container, ContainerControl).Controls(strNewSearchName)

    End Function

    Public Shared Function FindObject(ByRef Container As Control, ByVal SearchName As String, ByVal SearchType As ControlType) As ArrayList
        Try
            Dim arrObj As New ArrayList

            For Each obj As Control In Container.Controls
                If obj.Name.IndexOf(SearchName) >= 0 Then

                    If SearchType = ControlType.CT_BUTTON And Not TypeOf obj Is Button Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_TEXTBOX And Not TypeOf obj Is TextBox Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_LABEL And Not TypeOf obj Is Label Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_CHECKBOX And Not TypeOf obj Is CheckBox Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_GROUPBBOX And Not TypeOf obj Is GroupBox Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_TABPAGE And Not TypeOf obj Is TabPage Then
                        Continue For
                    ElseIf SearchType = ControlType.CT_COMBOBOX And Not TypeOf obj Is ComboBox Then
                        Continue For
                    End If

                    arrObj.Add(obj)
                End If
            Next
            FindObject = arrObj

        Catch ex As Exception
            SetErrorInfo(ex)
            FindObject = Nothing
        End Try
    End Function

End Class
'End Namespace

